package com.lijin.batchsys.framework.control;

import java.util.Date;

import org.apache.log4j.Logger;

import com.lijin.batchsys.framework.common.IBatchLogger;
import com.lijin.batchsys.framework.dao.model.BatchExecLogDetail;
import com.lijin.batchsys.framework.dao.model.StepInfo;
import com.lijin.batchsys.framework.enums.RunningStatus;

public class StepExecContext {
	
	private static final Logger logger = Logger.getLogger(StepExecContext.class);
	
	private BatchExecContext batchContext;
	private StepInfo stepInfo;
	private Date startTm;
	private Date endTm;
	private RunningStatus status;
	private String execMsg;
	private String errMsg;
	private IBatchLogger batchLogger;
	private BatchExecLogDetail batchExecLogDetail;
	
	public StepExecContext(BatchExecContext batchContext, StepInfo stepInfo, IBatchLogger batchLogger) {
		this.batchContext = batchContext;
		Date now = new Date();
		this.startTm = now;
		this.endTm = now;
		this.status = RunningStatus.RUNNING;
		this.execMsg = "";
		this.errMsg = "";
		this.stepInfo = stepInfo;
		this.batchLogger = batchLogger;
	}
	
	/**
	 * 记录上下文状态
	 */
	public void logContext() {
		if (batchLogger != null) {
			// 记录上下文状态的操作如果失败，则打印失败信息，但是不中断当前批量处理流程
			try {
				if (batchExecLogDetail == null) {
					batchLogger.logStepContext(this, false);
				} else {
					batchLogger.logStepContext(this, true);
				}
			} catch (Exception e) {
				logger.error("记录步骤执行上下文状态出错，步骤信息:" + this.stepInfo.getSummary(), e);
			}
		}
	}
	
	public BatchExecContext getBatchContext() {
		return batchContext;
	}
	public Date getStartTm() {
		return startTm;
	}
	public void setStartTm(Date startTm) {
		this.startTm = startTm;
	}
	public Date getEndTm() {
		return endTm;
	}
	public void setEndTm(Date endTm) {
		this.endTm = endTm;
	}
	public String getExecMsg() {
		return execMsg;
	}
	public void setExecMsg(String execMsg) {
		this.execMsg = execMsg;
	}
	public String getErrMsg() {
		return errMsg;
	}
	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}
	public RunningStatus getStatus() {
		return status;
	}
	public void setStatus(RunningStatus status) {
		this.status = status;
	}
	public StepInfo getStepInfo() {
		return stepInfo;
	}
	public void setStepInfo(StepInfo stepInfo) {
		this.stepInfo = stepInfo;
	}
	public String getStepSummary() {
		return this.getStepInfo().getSummary();
	}

	public BatchExecLogDetail getBatchExecLogDetail() {
		return batchExecLogDetail;
	}

	public void setBatchExecLogDetail(BatchExecLogDetail batchExecLogDetail) {
		this.batchExecLogDetail = batchExecLogDetail;
	}
}
